package com.write.program.gui;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Endstart
 * Date: 2021-02-08
 * Time: 16:46
 */
public class ReaderGUI extends JFrame implements MouseListener {
    private JTextArea textArea = null;
    private JPanel contentPanel = null;
    private Map<String, String> contentMap;

    public ReaderGUI(String title) {
        this.setTitle(title);
        this.setSize(600, 400);
        this.setLocationRelativeTo(null);//窗口居中显示 setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//        container.setLayout(new BorderLayout());

        this.add(initContent(), BorderLayout.WEST);
        this.add(initCenter(), BorderLayout.CENTER);

        this.setVisible(true);
    }


    public static void main(String[] args) {
        new ReaderGUI("阅读器");
    }

    private JComponent initContent() {
        contentPanel = initReadDataJPanel();
        return new JScrollPane(contentPanel);
    }

    public JComponent initCenter() {
        textArea = new JTextArea();
        textArea.setFont(new Font("仿宋", Font.BOLD + Font.PLAIN, 18));
        textArea.setEditable(false);
        return new JScrollPane(textArea);
    }

    private JPanel initReadDataJPanel() {
        contentMap = initReadData();
        JPanel jPanel = new JPanel();
        jPanel.setLayout(new GridLayout(contentMap.size(), 1));
        for (String title : contentMap.keySet()) {
            JLabel jLabel = new JLabel(title);
            jLabel.setFont(new Font("仿宋", Font.BOLD + Font.PLAIN, 18));
            jLabel.addMouseListener(this);
            jPanel.add(jLabel);
        }
        return jPanel;
    }

    private Map<String, String> initReadData() {
        contentMap = new LinkedHashMap<>();
        contentMap.put("咏鹅", "\n鹅，鹅，鹅，曲项向天歌。\n白毛浮绿水，红掌拨清波。");
        contentMap.put("静夜思", "\n床前明月光，疑是地上霜。\n举头望明月，低头思故乡。");
        contentMap.put("小池", "\n泉眼无声惜细流，树阴照水爱晴柔。\n小荷才露尖尖角，早有蜻蜓立上头。");
        contentMap.put("望庐山瀑布", "\n日照香炉生紫烟，遥看瀑布挂前川。\n飞流直下三千尺，疑是银河落九天。");
        contentMap.put("春晓", "\n春眠不觉晓，处处闻啼鸟。\n夜来风雨声，花落知多少。");
        return contentMap;
    }


    @Override
    public void mouseClicked(MouseEvent e) {

    }

    @Override
    public void mousePressed(MouseEvent e) {

    }

    @Override
    public void mouseReleased(MouseEvent e) {
        JLabel jLabel = (JLabel) e.getComponent();
        String title = jLabel.getText();
        if (contentMap.containsKey(title)) {
            textArea.setText(contentMap.get(title));
        }
    }

    @Override
    public void mouseEntered(MouseEvent e) {

    }

    @Override
    public void mouseExited(MouseEvent e) {

    }
}